package neusoft.edu.cn.a215lmeordering.activities;

import android.content.Intent;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import neusoft.edu.cn.a215lmeordering.Listener.ListListener;
import neusoft.edu.cn.a215lmeordering.Listener.Listener;
import neusoft.edu.cn.a215lmeordering.R;
import neusoft.edu.cn.a215lmeordering.adapter.ShopAdapter;
import neusoft.edu.cn.a215lmeordering.adapter.ShopFoodAdapter;
import neusoft.edu.cn.a215lmeordering.bean.Food;
import neusoft.edu.cn.a215lmeordering.bean.IsCollect;
import neusoft.edu.cn.a215lmeordering.bean.Shop;
import neusoft.edu.cn.a215lmeordering.iface.OnItemClickListener;
import neusoft.edu.cn.a215lmeordering.model.CollectionShopModel;
import neusoft.edu.cn.a215lmeordering.model.IsCollectModel;
import neusoft.edu.cn.a215lmeordering.model.ShopListModel;
import neusoft.edu.cn.a215lmeordering.model.ShopfoodListModel;

public class ShopFoodActivity extends AppCompatActivity {
    private List<Food> List;
    private IsCollect isCollect;
    RecyclerView recyclerView;
    ShopFoodAdapter viewAdapter;
    private int flag=0;
    private int shuaxin_flag=0;
    private int shop_id;
    private String shopname;
    private ImageView fanhui,collect;
    private TextView shopName;
    SwipeRefreshLayout swipeRefreshLayout;

    ListListener shopListListener = new ListListener() {

        @Override
        public void onResponse(List foodList) {
            List=foodList;
            viewAdapter.setDate(List);
            if(shuaxin_flag==1){
                flag=0;
                swipeRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(), "失败："+msg, Toast.LENGTH_SHORT).show();
        }
    };

    Listener listener = new Listener() {
        @Override
        public void onResponse(Object o) {
            isCollect = (IsCollect) o;
            System.out.println("iscollect:"+isCollect.getCollected());
            if(isCollect.getCollected().equals("1")){
                Picasso.get().load(R.drawable.shoucang1_32).into(collect);
            }else{
                Picasso.get().load(R.drawable.shoucang_32).into(collect);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    OnItemClickListener itemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Intent intent = new Intent(getApplicationContext(),FoodDetailsActivity.class);
            intent.putExtra("food_id",List.get(position).getFood_id());
            startActivity(intent);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();//去掉标题栏
        actionBar.hide();//去掉标题栏
        setContentView(R.layout.activity_shop_food);

        init();
        RecyclerViewInit();

        final Intent intent = getIntent();
        shop_id = intent.getIntExtra("shop_id",0);
        shopname = intent.getStringExtra("shopname");
        System.out.println("collect:shop_id:"+shop_id);
        ShopfoodListModel shopfoodListModel = new ShopfoodListModel();
        shopfoodListModel.getResultList(shop_id,shopListListener);

        IsCollection();

        shopName.setText(shopname);

        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CollectionShopModel collectionShopModel = new CollectionShopModel(view);
                collectionShopModel.getResultList(Integer.parseInt(LoginActivity.UserId),shop_id);
                IsCollection();
            }
        });

    }

    private void IsCollection() {
        IsCollectModel isCollectModel = new IsCollectModel();
        isCollectModel.getResultList(Integer.parseInt(LoginActivity.UserId),shop_id,flag,listener);
    }

    private void init() {
        fanhui=findViewById(R.id.fanhui);
        collect=findViewById(R.id.collect);
        shopName=findViewById(R.id.shopName);
    }

    private void RecyclerViewInit() {
        viewAdapter = new ShopFoodAdapter();
        viewAdapter.setOnItemClickListener(itemClickListener);

        recyclerView = findViewById(R.id.foodList);

        //布局管理器  可以设置显示数据的方式为垂直或者水平
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);

//        //添加自己设计的分割线
//        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getApplicationContext(),DividerItemDecoration.VERTICAL);
//        dividerItemDecoration.setDrawable(ContextCompat.getDrawable(getApplicationContext(),R.drawable.divider));
//        recyclerView.addItemDecoration(dividerItemDecoration);

        swipeRefreshLayout = findViewById(R.id.swipeRefresh);

        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary),
                getResources().getColor(R.color.colorAccent),
                getResources().getColor(R.color.colorPrimaryDark));

        swipeRefreshLayout.setProgressViewOffset(true,50,60);

        swipeRefreshLayout.setRefreshing(false);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                shuaxin_flag=1;
                ShopfoodListModel shopfoodListModel = new ShopfoodListModel();
                shopfoodListModel.getResultList(shop_id,shopListListener);

            }
        });

        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(viewAdapter);

    }
}
